package uns.fit.gis.ui;

import java.io.IOException;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.VideoControl;

import uns.fit.gis.MapDrawer;
import uns.fit.gis.NokiaKeyDef;
import uns.fit.gis.Util;
import uns.fit.gis.fileio.FileWrapper;
import uns.fit.gis.fileio.PhotoData;
import uns.fit.gis.i18n.LocaleManager;

public class VideoCanvas extends Canvas implements CommandListener {
	private MapDrawer midlet;
	private Player player;
	private VideoControl videoControl;

	private Command backCommand, captureCommand;
	private Command configCommand;
	private String photoDescription;
	private int width, height;

	public VideoCanvas(MapDrawer midlet) {
		width = getWidth();
		height = getHeight();
		this.midlet = midlet;

		backCommand = new Command(LocaleManager.getMessage("back"),
				Command.BACK, 1);
		captureCommand = new Command(LocaleManager.getMessage("capture"),
				Command.OK, 1);
		configCommand = new Command(LocaleManager.getMessage("configuration"),
				Command.OK, 1);
		addCommand(backCommand);
		addCommand(captureCommand);
		addCommand(configCommand);
		setCommandListener(this);

		showCamera();

		//photoDescription = "encoding=jpeg&type=exif";
		photoDescription = "encoding=jpeg&width=800&height=600"; // run well on Nokia
	}

	protected void paint(Graphics g) {
		g.setColor(255, 0, 0);
		g.drawRect(0, 0, width - 1, height - 1);
		g.drawRect(1, 1, width - 3, height - 3);
	}

	protected void keyPressed(int keyCode) {
		//midlet.showAlert(String.valueOf(keyCode));
		switch (keyCode) {
		case 53:
		case NokiaKeyDef.NOKIA_KEY_SELECT:
			new Thread() {
				public void run() {
					String dirPhotos = System
							.getProperty("fileconn.dir.photos");
					if (dirPhotos == null) {
						dirPhotos = Util.STORAGE_FOLDER;
					}
					byte[] photoData = null;
					try {
						photoData = videoControl.getSnapshot(photoDescription);
					} catch (MediaException me) {
						try {
							photoData = videoControl.getSnapshot(null);
						} catch (MediaException me2) {
							me2.printStackTrace();
						}
					}
					player.close();
					player = null;
					videoControl = null;

					StringBuffer strBuf = new StringBuffer();
					strBuf.append(dirPhotos);

					System.out.println(strBuf.toString());

					strBuf.append("GeoImage");
					/*
					Date currentDate = new Date();
					strBuf.append(currentDate);
					 */
					strBuf.append(System.currentTimeMillis());
					strBuf.append(".jpg");
					System.out.println(strBuf.toString());
					FileWrapper.save(new PhotoData(photoData, 0, 0), strBuf
							.toString());
					//MapDrawer.showAlert(LocaleManager.getMessage("saveto")
					//		+ " " + dirPhotos);
					midlet.showPhotoForm(photoData);
					//midlet.show(new PreviewPhotoForm(photoData));

				}
			}.start();
			break;
		default:
			break;
		}
	}

	public void commandAction(Command c, Displayable s) {
		if (c == backCommand) {
			if (player != null) {
				player.close();
				player = null;
				videoControl = null;
			}
			midlet.showMapCanvas();
		} else if (c == captureCommand) {
			keyPressed(NokiaKeyDef.NOKIA_KEY_SELECT);
		} else if (c == configCommand) {
			midlet.showConfigForm();
		}
	}

	public void showCamera() {
		try {
			try {
				player = Manager.createPlayer("capture://image");
			} catch (Exception e) {
				e.printStackTrace();
				player = Manager.createPlayer("capture://video");
			}

			player.realize();

			videoControl = (VideoControl) player.getControl("VideoControl");
			player.start();

			videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, this);
			try {
				videoControl.setDisplayLocation(2, 2);
				videoControl.setDisplaySize(width - 4, height - 4);
			} catch (MediaException me) {
				me.printStackTrace();
			}
			videoControl.setVisible(true);
		} catch (IOException ioe) {
			ioe.printStackTrace();
		} catch (MediaException me) {
			me.printStackTrace();
		}
	}

	public String getPhotoDescription() {
		return photoDescription;
	}

	public void setPhotoDescription(String photoDescription) {
		this.photoDescription = photoDescription;
	}
}
